package com.hospital.register.order.service.impl;

import com.hospital.register.dto.order.PaymentListDto;
import com.hospital.register.enums.PaymentStatusEnum;
import com.hospital.register.enums.PaymentTypeEnum;
import com.hospital.register.model.order.PaymentInfo;
import com.hospital.register.order.mapper.PaymentInfoMapper;
import com.hospital.register.order.service.PaymentInfoService;
import com.hospital.register.vo.order.OrderQueryVo;
import com.hospital.register.vo.order.PaymentListVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    /**
     * 访问支付记录表
     */
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Override
    public boolean insert(PaymentInfo paymentInfo) {
        return this.paymentInfoMapper.insert(paymentInfo) == 1;
    }

            /**
         * 如果支付中生成了多张二维码，微信会不会重复扣款呢？
         * 如果该订单已经生成了支付宝的二维码，是否应该生成微信的支付二维码呢？
         * 如果生成，如何避免重复支付呢？
         */
    @Override
    public boolean insert(OrderQueryVo order, Byte paymentType) {
        // 查询该订单是否已有支付记录
        PaymentListDto repeatConditions = new PaymentListDto();
        repeatConditions.setOrderId(order.getId());
        repeatConditions.setPaymentType(PaymentTypeEnum.WEIXIN.getStatus());
        repeatConditions.setFilterFail(true);// 不包含失败记录
        repeatConditions.setUnique(true);

        List<PaymentListVo> paymentList = this.list(repeatConditions);
        if (!paymentList.isEmpty()) {// 该订单有相同类型的记录，不用生成
            return true;
        }

        StringBuilder subject = new StringBuilder("您即将预约")
                .append(new DateTime(order.getReserveDate()).toString("yyyy-MM-dd")).append(" ")
                .append(order.getHosname()).append("医院 ")
                .append(order.getDepname()).append(" ")
                .append(order.getTitle()).append(" 就诊");

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(order.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        paymentInfo.setSubject(subject.toString());
        paymentInfo.setTotalAmount(order.getAmount());

        return this.insert(paymentInfo);
    }

    @Override
    public List<PaymentListVo> list(PaymentListDto conditions) {
        return this.paymentInfoMapper.list(conditions);
    }

    @Override
    public Long selectIdByConditions(Long orderId, Byte paymentType, Byte paymentStatus) {
        return this.paymentInfoMapper.selectIdByConditions(orderId, paymentType, paymentStatus);
    }

    @Override
    public boolean updateById(PaymentInfo paymentInfo) {
        return paymentInfo.willUpdateById()
                && this.paymentInfoMapper.updateById(paymentInfo) == 1;
    }

}
